var app = getApp();
Page({

   
  data: {
    height: '',
    res: [],
    loadingTxt: '',
    noMoreData: false,
    page: 1,
    size: 16,
    resArr: "",
    option:"",
    count: "",
    status:""
  },

  orderList(options="") {
    var that = this;
    var page = this.data.page;
    var size = this.data.size;
    let url = app.globalData.url +'/index.php?_m=checkout/order/listing&v=v20190909b&status=' + options +'&page='+page+'&size='+size ;
    app.getUserToken(function (err, token) {
      wx.showLoading({
        title: '加载中...',
      })
      wx.request({
        url: url,
        method: 'POST',
        data: {auth_token: token },
        header: { 'content-type': 'application/x-www-form-urlencoded' },
        success: (res) => {
          wx.hideLoading();
          if (res.data.status == 401) {
            // 清空授权token
            app.globalData.token = null
            wx.removeStorageSync('token')
            that.orderList();
          } else if (res.data.code == 0){

            if (res.data.data.list.length == 0){
              wx.showToast({
                title: '无货物显示',
                icon: 'loading',
                duration: 2000
              });
              setTimeout(function () {
                wx.hideToast
              }, 2000)
            }
          
            let goods = [];
            if (page == 1) {
              goods = res.data.data.list;
            } else {
              goods = that.data.res.concat(res.data.data.list);
            }

            that.setData({
              res: goods,
              option: options ? options:""
            });
            console.log(res.data.data.list.length)
            if (res.data.data.list.length < that.data.size) {

              that.setData({

                loadingTxt: '没有更多内容',

                noMoreData: true,

              });

            }
       
          }
        }
      })
    })
  },

  onLoad: function (options) {
    this.orderList(options.status);
    
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          height: res.windowHeight
        })
      }
    })
  },

  onPullDownRefresh: function () {
    wx.showToast({
      title: '加载中...',
      icon: 'loading'
    })

    let status=this.data.option;

    // 初始化信息
    this.orderList(status);

    setTimeout(function () {
      wx.stopPullDownRefresh()
    }, 1000)

  },

  onReachBottom: function () {

    if (!this.data.noMoreData) {

      let status = this.data.option;
      this.data.page = this.data.page + 1;

      this.orderList(status);

    };

  },

  onShareAppMessage: function () {
    app.globalData.uid = app.globalData.uid || wx.getStorageSync('uid') || 0;
    return {
      title: '深圳智汇创想科技有限责任公司员工福利购!',
      path: '/pages/index/index?fid=' + app.globalData.uid
    }
  }
  
})